package com.sendinfo.xwork.converter;

import org.apache.commons.beanutils.ConversionException;

@SuppressWarnings("unchecked")
public class BooleanConverter extends CustomizeConverter {
	@Override
	protected Object fromString(String value, Class clazz) {
		String stringValue = value.toString().toLowerCase();
		for (int i = 0; i < trueStrings.length; i++)
			if (trueStrings[i].equals(stringValue))
				return Boolean.TRUE;

		for (int i = 0; i < falseStrings.length; i++)
			if (falseStrings[i].equals(stringValue))
				return Boolean.FALSE;

		for (int i = 0; i < nullStrings.length; i++)
			if (nullStrings[i].equals(stringValue))
				return null;

		throw new ConversionException("Can't convert value '" + value
				+ "' to a Boolean");
	}

	private String trueStrings[] = { "true", "yes", "y", "on", "1" };
	private String falseStrings[] = { "false", "no", "n", "off", "0" };
	private String nullStrings[] = { "null", "" };
}
